# 数据库
import pymysql


class DB:
    def __init__(self):
        self.host = 'localhost'
        self.port = int(3306)
        self.user = 'root'
        self.password = '123456'
        self.database = 'finance'
        self.connection = None
        self.cursor = None

    def connect(self):
        # 连接数据库
        self.connection = pymysql.connect(host=self.host, user=self.user, password=self.password,
                                          port=self.port, charset='utf8mb4',
                                          database=self.database)
        self.cursor = self.connection.cursor()

    def close(self):
        self.cursor.close()

    def execute(self, sql, params=None):
        if params is None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)
        self.connection.commit()
        return self.cursor.fetchall()

